package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Genero;
import com.lostreseditores.sabersai.model.dto.GeneroDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class GeneroView {
    private InputText txtGenedescripv;
    private InputText txtGenesiglav;
    private InputText txtGeneidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<GeneroDTO> data;
    private GeneroDTO selectedGenero;

    public GeneroView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            GeneroDTO generoDTO = (GeneroDTO) e.getObject();

            if (txtGenedescripv == null) {
                txtGenedescripv = new InputText();
            }

            txtGenedescripv.setValue(generoDTO.getGenedescripv());

            if (txtGenesiglav == null) {
                txtGenesiglav = new InputText();
            }

            txtGenesiglav.setValue(generoDTO.getGenesiglav());

            if (txtGeneidn == null) {
                txtGeneidn = new InputText();
            }

            txtGeneidn.setValue(generoDTO.getGeneidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtGenedescripv != null) {
            txtGenedescripv.setValue(null);
            txtGenedescripv.setDisabled(true);
        }

        if (txtGenesiglav != null) {
            txtGenesiglav.setValue(null);
            txtGenesiglav.setDisabled(true);
        }

        if (txtGeneidn != null) {
            txtGeneidn.setValue(null);
            txtGeneidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Genero entity = null;

        try {
            Integer geneidn = new Integer(txtGeneidn.getValue().toString());
            entity = BusinessDelegatorView.getGenero(geneidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtGenedescripv.setDisabled(false);
            txtGenesiglav.setDisabled(false);
            txtGeneidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtGenedescripv.setValue(entity.getGenedescripv());
            txtGenedescripv.setDisabled(false);
            txtGenesiglav.setValue(entity.getGenesiglav());
            txtGenesiglav.setDisabled(false);
            txtGeneidn.setValue(entity.getGeneidn());
            txtGeneidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveGenero(FacesUtils.checkString(
                    txtGenedescripv), FacesUtils.checkInteger(txtGeneidn),
                FacesUtils.checkString(txtGenesiglav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteGenero(FacesUtils.checkInteger(
                    txtGeneidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateGenero(FacesUtils.checkString(
                    txtGenedescripv), FacesUtils.checkInteger(txtGeneidn),
                FacesUtils.checkString(txtGenesiglav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtGeneidn == null) {
                txtGeneidn = new InputText();
            }

            txtGeneidn.setValue(selectedGenero.getGeneidn());

            BusinessDelegatorView.deleteGenero(FacesUtils.checkInteger(
                    txtGeneidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedGenero);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String genedescripv, Integer geneidn,
        String genesiglav) throws Exception {
        try {
            BusinessDelegatorView.updateGenero(genedescripv, geneidn, genesiglav);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("GeneroView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtGenedescripv() {
        return txtGenedescripv;
    }

    public void setTxtGenedescripv(InputText txtGenedescripv) {
        this.txtGenedescripv = txtGenedescripv;
    }

    public InputText getTxtGenesiglav() {
        return txtGenesiglav;
    }

    public void setTxtGenesiglav(InputText txtGenesiglav) {
        this.txtGenesiglav = txtGenesiglav;
    }

    public InputText getTxtGeneidn() {
        return txtGeneidn;
    }

    public void setTxtGeneidn(InputText txtGeneidn) {
        this.txtGeneidn = txtGeneidn;
    }

    public List<GeneroDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataGenero();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<GeneroDTO> generoDTO) {
        this.data = generoDTO;
    }

    public GeneroDTO getSelectedGenero() {
        return selectedGenero;
    }

    public void setSelectedGenero(GeneroDTO genero) {
        this.selectedGenero = genero;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
